/**
 * Created by shanming on 2016/7/31.
 */
app.service('goodsClass',['$http','uiService','$q',function($http,uiService,$q){
	// $http.defaults.headers.post["Content-Type"] = "application/x-www-form-urlencoded";
	//  $http.defaults.headers.post["Content-Type"] = "application/x-www-form-urlencoded";
	var self = this;

	this.goodsDetail = {
		goodsCategery:null,
		goodsLabel:null,
		goodsBrand:null

	};

   //添加标签
    this.addProps=function(obj){
    	
    	var key=obj.key;
    	if(key == "classes"){
    		var classFication={
    				name:obj.addValue
    		}
    		var promise=$http.get("/dictionary/savaClassicication",$.param(classFication)).then(function(success){
        		if(success.code ==1){
        			uiService.alert('添加成功','success');
        		}
        	});
    	}
    	if(key == "label"){
    		var dicTags={
    				name:obj.addValue
    		}
    		var promise=$http.get("/dictionary/saveGoodsTag",$.param(dicTags)).then(function(success){
    			if(success.code ==1){
        			uiService.alert('添加成功','success');
        		}
        	});
    	}
    	
    	if(key == "brand"){
    		var dicBrand={
    				name:obj.addValue
    		}
    		var promise=$http.get("/dictionary/saveDicBrand",$.param(dicBrand)).then(function(success){
    			if(success.code ==1){
        			uiService.alert('添加成功','success');
        		}else{
        			uiService.alert(success.message,'success');
        		}
        	});
    	}
    }

    //添加商品
    this.addnewGood=function(obj){
        
   	    //商品品牌
	    dicBrandDto=obj.goodBrands;
	    
	    //商品标签
	    dicTagsDto=obj.goodLabel;
	    
	    //商品类别
	    dicClassificationsDto=obj.goodsClassification;
	    
	    //面料材质

	    //面料品牌

	    //库存
        var goodsColorNum=obj.goodsColorNum;
        
        var goodsColorMessage = [];
        
        for(var i=0;i<goodsColorNum.length;i++){
        	var sizeMessage = goodsColorNum[i].size;
        	for(var j=0;j<sizeMessage.length;j++){
        		var temp = {};
            	temp.color = goodsColorNum.Name;
            	temp.size = sizeMessage[j].name;
            	temp.stockQuantity = sizeMessage.num;
            	goodsColorMessage.push(temp);
        	}
        	
        }


        
	    
   	var GoodsDTO={
   			name:obj.name,
		    userId:"111",
		goodsCheckStatus:"11",
			materialFabric:obj.goodsFabric,
			materialLining:obj.goodElasticity,
			materialPaittern:obj.goodThickness,
			materialElastomer:"",
			materialBrand:"",
			materialThickness:"",

   			tagPrice:obj.tagPrice,
   			buyingPrice:obj.storagePrice,
            description:obj.goodDescription,
   			dicBrandDtoNames:angular.toJson(dicBrandDto),
   			dicTagsDtoNames:angular.toJson(dicTagsDto),
   			dicClassificationsDtoNames:angular.toJson(dicClassificationsDto),
		    goodYadageStrings:angular.toJson(obj.goodsColorNum)
   	}

		/*private Long id;

		 private Long goodsCheckStatus;

		 private Long userId;

		 private String name;

		 private BigDecimal buyingPrice;

		 private BigDecimal tagPrice;

		 private BigDecimal salePrice;

		 private String materialFabric;

		 private String materialLining;

		 private String materialPaittern;

		 private String materialElastomer;

		 private String materialBrand;

		 private String materialThickness;

		 private String description;*/

	/*	console.log(angular.toJson(GoodsDTO));
		var param = angular.toJson(GoodsDTO);*/
	var param = angular.copy(GoodsDTO);
		// param=JSON.stringify(param);
		console.log(param);
		// var promise=$http.post("/goods/saveGoods",$httpParamSerializerJQLike(param));
   	var promise=$http({
		url:'/goods/saveGoods',
		method:'POST',
		data:param,
		// headers: { "Content-Type": "application/x-www-form-urlencoded" }
	});
       promise.then(
           function(success){
			 /*  console.log(success);*/
               if(success.code ==0){
               	 uiService.alert('添加成功','success');
               }else{
               	 uiService.alert(success.message,'fail');
               }
           },
           function(error){
               console.log(error);
               deferred.reject(error);
           });
   	
       uiService.alert('添加成功','success');
      /* console.log(obj);*/
    };
    
    
    //商品分类
    this.getClassicications = function () {
		return $http.get("/dictionary/getClassicications").then(function(success){
			self.goodsDetail.goodsCategery=success.data;
		});
	};
     //商品标签
	this.getGoodsTags = function () {
		return $http.get("/dictionary/getGoodsTags").then(function(success){
			self.goodsDetail.goodsLabel=success.data;
		});
	};
    //商品品牌
	this.getDicBrands = function () {
		return $http.get("/dictionary/getDicBrands").then(function(success){
			self.goodsDetail.goodsBrand=success.data;
		});
	};

	 //面料材质
	this.getMaterialCottons = function () {
		return $http.get("/dictionary/getMaterialCottons").then(function(success){
			self.goodsDetail.goodsFabric=success.data;
		});
	};
	
    //面料品牌
	this.getMateriaMaterialBrands = function () {
		return $http.get("/dictionary/getDicMaterialBrands").then(function(success){
			self.goodsDetail.goodsFbrands=success.data;
		});
	};


    //初始化加载数据
    this.getgoodMessage=function(){
		return $q.all([self.getClassicications(),self.getGoodsTags(),self.getDicBrands(),self.getMaterialCottons(),self.getMateriaMaterialBrands()]);

     /*  return $http.get('js/app/goods/json/list.json').then(function (data) {
           return data.data;
       })*/
    }
    
    
    function getGoodsList(pagingOptions){
        var queryCondition={
            status:pagingOptions.status,        		
            name:pagingOptions.goodsName,
            orderStartTime:startTime,
           /* orderEndTime:endTime,           
            tag:label,*/
            pageNo:pagingOptions.currentPage,
            pageSize:pagingOptions.pageSize,
            pageSizes:pagingOptions.pageSizes
        };
        return $http.post("/goods/getGoodsList",$.param(queryCondition)).then(function(data){
        	console.log(data);
        	return data;
        });
        //var promise=$http.get("js/app/orders/json/deliverGoodsOrderList.json");
      
        
       /* private int pageSize;
        private int currentPage;
        private int totalServerItems;
        private List<TModel> data;*/
    };
    
    

    this.getGoodMessage=function(goodid){
        return $http.get('js/app/goods/json/good.json').then(function (data) {
            return data.data;
        })
    }
    
    




}])